package com.kgc.fk.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.collection.pojo.OtherPay;
import com.kgc.collection.pojo.Refund;
import com.kgc.collection.pojo.RefundDetail;
import com.kgc.collection.pojo.RefundOrder;
import com.kgc.fk.dao.RefundDao;
import com.kgc.fk.service.RefundService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class RefundServiceImpl implements RefundService {

    private Logger logger = LoggerFactory.getLogger(RefundServiceImpl.class);

    @Resource
    private RefundDao refundDao;


    @Override
    public ResponseMsg getRefundPage(Map<String, Object> map) {
        logger.info("RefundServiceImpl getRefundPage map={}", map);
        ResponseMsg responseMsg = new ResponseMsg();
        int pageNum = 0;
        int pageSize = 8;
        if(map.get("pageNum") instanceof String){
            pageNum =Integer.parseInt((String) map.get("pageNum"));
            pageSize =Integer.parseInt((String) map.get("pageSize"));
        }else {
            pageNum = (Integer) map.get("pageNum");
            pageSize = (Integer) map.get("pageSize");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Refund> refundList = refundDao.getRefundList(map);
        PageInfo<Refund> pageInfo = new PageInfo<>(refundList);
        responseMsg.setData(pageInfo);
        responseMsg.setCode(200);
        logger.info("RefundServiceImpl getRefundPage map{} pageInfo={}", map,pageInfo);
        return responseMsg;
    }

    @Override
    public ResponseMsg getRefundOrder() {
        logger.info("RefundServiceImpl getRefundOrder");
        ResponseMsg responseMsg = new ResponseMsg();
        List<RefundOrder> list = refundDao.getRefundType();
        responseMsg.setData(list);
        responseMsg.setCode(200);
        logger.info("RefundServiceImpl getRefundOrder responseMsg={}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg updateRefund(Map<String, Object> params) {
        logger.info("RefundServiceImpl updateRefund params={}", params);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        int result = refundDao.updateRefund(params);
        if (result > 0) {
            responseMsg.setCode(200);
        }
        logger.info("RefundServiceImpl updateRefund params{} result={}",params, result);
        return responseMsg;

    }

    @Override
    public ResponseMsg addRefund(Map<String, Object> params) {
        logger.info("addRefund params:" + params);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        String code = (String) params.get("code");
        ArrayList<Map<String, Object>> infoList =(ArrayList<Map<String, Object>>) params.get("list");
        Date date = new Date();
        params.put("createDate", date);
        int result = refundDao.addRefund(params);
        logger.info("addRefund refund params{} result{}" ,params, result);
        if (result <= 0) {
            return responseMsg;
        }

        for (Map<String, Object> map : infoList) {
            map.put("majorCode", code);
            map.put("createDate", date);
            map.put("createId", params.get("modiId"));
            result = refundDao.addRefundDetail(map);
            if (result <= 0) {
                responseMsg.setMsg("添加详情错误");
                return responseMsg;
            }
        }
        logger.info("addRefund refundInfo params{} result{}" ,infoList, result);
        responseMsg.setCode(200);
        return responseMsg;
    }

    @Override
    public ResponseMsg getRefundByMap(Map<String, Object> params) {
        logger.info("getRefundByMap params:" + params);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);

        List<Refund> list = refundDao.getRefundList(params);
        logger.info("getRefundByMap params{} list{}" ,params, list);
        for (Refund refund : list) {
            String code = refund.getCode() ;
            List<RefundDetail> details = refundDao.getRefundDetail(code);
            refund.setList(details);
        }
        logger.info("getRefundByMap params{} result{}" ,params, list.get(0));
        responseMsg.setData(list.get(0));
        responseMsg.setCode(200);
        return responseMsg;
    }

    @Override
    public ResponseMsg modiRefund(Map<String, Object> params) {
        logger.info("modiRefund params:" + params);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        String code = (String) params.get("code");
        ArrayList<Map<String, Object>> infoList =(ArrayList<Map<String, Object>>) params.get("list");
        Date date = new Date();
        params.put("modiDate", date);
        int result = refundDao.modiRefund(params);

        logger.info("modiRefund params{} result{}" ,params, result);
        result = refundDao.delRefundDetail(code);
        logger.info("delRefundDetail params{} result{}" ,infoList, result);
        if (result <= 0) {
            return responseMsg;
        }
        for (Map<String, Object> map : infoList) {
            map.put("majorCode", code);
            map.put("createDate", date);
            map.put("modiId", params.get("modiId"));
            result = refundDao.addRefundDetail(map);
            if (result <= 0) {
                return responseMsg;
            }
        }
        logger.info("modiRefund params{} result{}" ,infoList, result);
        responseMsg.setCode(200);
        return responseMsg;
    }


}
